<?php ob_start(); session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quản lý bình chọn</title>
<link href="../Twitter_Bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="../CSS/css_admin.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="../CSS/validationEngine.jquery.css"/>

<script type="text/javascript" src="../Twitter_Bootstrap/js/jquery.js"></script>
<script type="text/javascript" src="../Twitter_Bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../Scripts/jquery.validationEngine.js"></script>
<script type="text/javascript" src="../Scripts/jquery.validationEngine-vi.js"></script>
<script type="text/javascript" src="../Scripts/VoteManagementJS.js"></script>


<style type="text/css">
<!--
#tbResult td,#tbResult th
{
	text-align:center;
}
-->
</style>
</head>

<body>
<div class="container" id="Container">
<!--Menu top -->
  <div id="menu">
  	<?php
		unset($_SESSION['addvote']);
		unset($_SESSION['delvote']);
		include("MainMenu.php");
		include("../Connections/connect_admin.php");
		$page=1;
		$recordPerPage=10;
		if(isset($_GET["page"]))
			{
				$page=intval($_GET["page"]);
			}
		$index=($page-1)*$recordPerPage;
		$keyword='';
		$topic=0;
		 if(isset($_GET["keyword"]))
	  {
		$keyword=trim($_GET["keyword"]);
		$topic=intval($_GET["topic"]);
		
		$query="SELECT v.VoteID, v.TopicID, v.VoteName, v.Content, v.CreatedDate,v.IsEnd,v.CreatedUser, v.EndDate,t.TopicName FROM vote v INNER JOIN topic t ON t.TopicID = v.TopicID WHERE v.IsDeleted=0 AND (v.VoteName LIKE '%$keyword%' OR v.Content LIKE '%$keyword%') AND (v.TopicID=$topic OR $topic=0) ORDER BY v.CreatedDate DESC ";
	  }
	  else
	  {
		 $query="SELECT v.VoteID, v.TopicID, v.VoteName, v.Content, v.CreatedDate,v.IsEnd, v.EndDate,t.TopicName,v.CreatedUser FROM vote v INNER JOIN topic t ON t.TopicID = v.TopicID WHERE v.IsDeleted=0 ORDER BY v.CreatedDate DESC ";
	  }
	  $sql=$query." Limit $index,$recordPerPage";
	  $result=mysql_query($sql);
	?>
    </div>
  </div>
  <div id="divContain">
  <!--Content -->
   <div class="divTitle"><span class="titlePage" >Quản lý bình chọn</span></div>
  	<div style="padding-top:30px" >
     <form action="" method="get" id="formSearch" class="form-horizontal">
 		
        <input type="text" id="txtKeyword" name="keyword" placeholder="Từ khóa..." <?php if($keyword!='') echo 'value="'.$keyword.'"'; ?>>
        <select name="topic" id="cbxTopic">
        	<option value="0">---Tất cả chuyên mục---</option>
             <?php	  
				function Menu($cap = 0,$gach = '&gt;&gt;  ', $arr = NULL){  
				if(!$arr) $arr = array();//khoi tao 1 array co ten la arr  
				$sql = mysql_query("SELECT * FROM topic WHERE IsDeleted = 0 and ParentID = ".$cap." order by OrderIndex");  
				while($row = mysql_fetch_assoc($sql)){  
				$arr[] = array('id'=>$row['TopicID'],'ten'=>$gach.$row['TopicName']);  
				$arr = Menu($row['TopicID'],$gach.'&gt;&gt;   ',$arr);  
				}  
				return $arr;  
				}  
				$Menu = Menu(0);  
		
				 foreach($Menu as $k=>$row)
				 {
			?>
				 <option value="<?php echo $row["id"]; ?>" <?php if($topic==$row["id"]) echo 'selected="selected"'; ?>><?php echo $row["ten"]; ?></option>
			<?php
				 }
			?>
        </select>
        <button class="btn" type="submit" id="btnSearch"><i class=" icon-search"></i> Tìm kiếm</button>
       
        
         <a type="button" class="btn btn-primary pull-right" id="btnAddVote" href="#updateVote" data-toggle="modal"><i class="icon-white icon-plus-sign"></i> Tạo bình chọn</a>
    </form>
    
     <?php
	if(isset($_GET["e"]))
	{
		$error=intval($_GET["e"]);
		switch($error)
		{
			case 0:
	?>
    <div class="alert alert-success">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Thêm mới bình chọn THÀNH CÔNG</strong>
    </div>
    <?php
			break;
			case 1:
	?>
     <div class="alert alert-error">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Thêm mới bình chọn THẤT BẠI</strong>
    </div>
     <?php
	 		break;
			case 2:
	?>
     <div class="alert alert-success">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Cập nhật bài bình chọn THÀNH CÔNG</strong>
    </div>
     <?php
	 		break;
			case 3:
	?>
     <div class="alert alert-error">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Cập nhật bình chọn THẤT BẠI</strong>
    </div>
    <?php
			break;
		}	
		
	}
	?>
    
    <table class="table table-bordered table-striped  " id="tbResult" width="100%" border="0" cellspacing="0" cellpadding="0" >
  <tr>
  	<th scope="col" class="hide">Mã</th>
  	<th scope="col">Chủ đề</th>
    <th scope="col">Chuyên mục</th>
    <th scope="col">Người tạo</th>
    <th scope="col">Ngày tạo</th>
    <th scope="col">Kết thúc</th>
    <th scope="col">Ngày kết thúc</th>
    <th scope="col">&nbsp;</th>
    <th scope="col">&nbsp;</th>
  </tr>
    <?php
  	
	 
	  while($row=mysql_fetch_assoc($result))
	  {
	  ?>	
	 <tr>
    <td class="hide"><?php echo $row['VoteID']; ?></td>
    <td><?php echo $row['VoteName']; ?></td>
    <td><?php echo $row['TopicName']; ?></td>
    <td><?php echo $row['CreatedUser']; ?></td>
    <td><?php $sqldate=strtotime($row["CreatedDate"]); $date=date('d/m/Y',$sqldate); echo $date; ?></td>
    <td><input type="checkbox" <?php if($row['IsEnd']==1){ ?>checked="checked" <?php }?> disabled="disabled" /></td>
    <td>
	<?php
	if($row["EndDate"]!='') 
	{
	$sqldate=strtotime($row["EndDate"]); $date=date('d/m/Y',$sqldate); echo $date; 
	}
	?>
    </td>
    <td><a href="#" title="Xóa" class="delete"><img src="../Images/Icons/delete.png" alt="delete"/></a></td>
    <td><a href="#updateVote" data-toggle="modal" title="Sửa" class="update"><img src="../Images/Icons/page_edit.png" alt="update" /></a></td>
   
  </tr>
  <?php }?>
</table>
	<div class="pagination pull-right">
 		<?php require("process/funcPaging.php");
			
			if(isset($_GET["keyword"]))
			{
				$keyword=$_GET["keyword"];
				$topic=intval($_GET["topic"]);
				$link="Vote.php?keyword=".$keyword."&topic=".$topic."&";
			}
			else
			{	
				$link="Vote.php?";
			}
			$resAll=mysql_query($query);
			$total=mysql_num_rows($resAll);
			
			Paging($total,$page,5,$recordPerPage,$link);
 		?>  
    </div>
	</div>
  <!--footer -->
  </div>
  <div id="footer">
     <?php
     	include("Footer.php");
	 ?>
  </div>
</div>
<!---------------------------Popup Thêm/Cập nhật bình chọn----------------------------->
    <div class="modal hide fade"  id="updateVote">
    <div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h3></h3>
    </div>
    <form action="process/Vote_process.php" method="post" id="formVote" style="margin-bottom:0px">
    <div class="modal-body">
    <input type="hidden" name="task" id="task" />
    <input type="hidden" name="voteid" id="voteid" />
    <p>
    	
        	<table width="100%" border="0" cellspacing="0" cellpadding="0">
            	<tr>
                    <td>Chuyên mục:</td>
                    <td>
                        <select  id="cbxTopicVote" class="validate[required]" name="topic">
                            <option value="">---Chọn chuyên mục---</option>
                            <?php	  
								
								$Menu2 = Menu(0);  
						
								 foreach($Menu2 as $k=>$row)
								 {
							?>
								 <option value="<?php echo $row["id"]; ?>" <?php if(isset($_GET["topic"])&&$_GET["topic"]==$row["id"]) echo 'selected="selected"'; ?>><?php echo $row["ten"]; ?></option>
							<?php
								 }
							?>
                        </select>
                    </td>
                    <td>
                        <label class="checkbox span2" id="lblEnd"><input type="checkbox" id="chkIsEnd" value="end" name="isend" /> Kết thúc</label>
                    </td>
                  </tr>
                  <tr>
                    <td>Chủ đề:</td>
                    <td colspan="2"><input name="votename" type="text" id="txtVoteName" class="validate[required]" style=" width:98%" /></td>
                  </tr>
                  <tr>
                    <td >Câu hỏi:</td>
                    <td colspan="2"><textarea id="txtQuestion" name="content" rows="3" style="width:98%" class="validate[required]"></textarea></td>
                  </tr>
                  <tr>
                    <td>Trả lời:</td>
                    <td colspan="2">
                    	<table width="100%" border="0" cellspacing="0" cellpadding="0" class="table table-bordered table-striped table-condensed" id="tbDetail"  >
                          <tr>
                            <th scope="col" class="hide">Mã</th>
                        
                            <th scope="col">Nội dung</th>
                            <th scope="col">Số lượt đồng ý</th>
                            <th scope="col">tỷ lệ %</th>
                            <th scope="col">&nbsp;</th>
                          </tr>
                         
                         <!-- <tr>
                            <td class="hide">&nbsp;</td>
                           
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>
                            <a href="#" title="Xóa"><img src="../Images/Icons/delete.png" alt="delete" onclick="confirm('Bạn thực sự muốn xóa?');" /></a>
                            </td>
                          </tr> -->
						</table>


                    </td>
                  </tr>
                  
              </table>
     <p>
     Tổng cộng: <strong id="txtSum">0</strong> lượt người.
     </p>
     <p>
     <button type="button" id="btnAddAnswer" class="btn btn-primary"><i class="icon-white icon-plus-sign"></i> Thêm câu trả lời</button>
     </p>

       
    </p>
    </div>
    <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Đóng lại</a>
    <button type="submit" class="btn btn-primary" id="btnSave">Cập nhật</button>
    </div>
     </form>
     <script>
		//-----------------------Validation----------------------------------
			$(document).ready(function(){
							   $("#formVote").validationEngine();
							   });
		</script>
    </div>
</body>
</html>
